<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    formType:0,
    title:''
  })
  interface formQuert {
    id:number
    room_id:number | string
    sort:number
    start_time:number
    end_time:number
    is_show:number | string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    room_id: [{ required: true, trigger: 'blur', message: '请输入房间ID' }],
    sort: [{ required: true, trigger: 'blur', message: '请输入排序数' }],
    start_time: [{ required: true, trigger: 'blur', message: '请选择开始时间' }],
    end_time: [{ required: true, trigger: 'blur', message: '请选择结束时间' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id=0 } = form
      await guild_roomApi.suggestAddorupdate(form).then(res=>{
        submitSet = true
        if(id) {
          ToastSuccess('修改成功')
        }else {
          ToastSuccess('新增成功')
        }
      }).catch(()=>{
        loading.close()
      })

      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = (query:any = {})=> {
    const {
      id,
      room_id,
      sort,
      end_timestamp:end_time,
      start_timestamp:start_time,
      room_name='',is_show=1
    } = query
    let formQuery:any = {
      room_id,
      sort,
      start_time,
      end_time,
      is_show
    }
    if(room_id) {
      formQuery.id = id
      action.title = `编辑${room_name}首页推荐位`
      action.formType = 1
    }else {
      action.formType = 0
      action.title = '新增首页推荐位'
    }
    Object.assign(form,formQuery)
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="40%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="房间ID" prop="room_id">
          <el-input v-model="form.room_id" placeholder="请输入房间ID" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="排序" prop="sort">
          <el-input type="number" v-model="form.sort" placeholder="请输入房间排序数" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="开始时间" prop="start_time">
          <el-date-picker
            v-model="form.start_time"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择开始时间"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="结束时间" prop="end_time">
          <el-date-picker
            v-model="form.end_time"
            type="datetime"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择结束时间"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="显示状态" prop="end_time">
          <el-select v-model="form.is_show" placeholder="请选择显示状态">
            <el-option label="显示"  :value="1" />
            <el-option label="不显示"  :value="0" />
          </el-select>
        </el-form-item>
      </el-form>

      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>